<?php

error_reporting(E_ALL);

include 'iniFile.class.php';
include 'dbManipulator.class.php';

$site_path = realpath(dirname(__FILE__) . '/../');
$iniconfig = new iniFile('config.ini');
try {
	$module = $iniconfig->getRule(array('module'));
} catch(Exception $e) {
	include $site_path . '/include/' . 'Exception.php';
	die();
}
if ($module['module'] == 'true') {
	$site_path = $site_path . '/modules/';
	define('__MODULE', 'true');
} else {
	define('__MODULE', 'false');
}
define('__SITE_PATH', $site_path);
define('__SITE_APPLICATION_PATH', realpath(dirname(__FILE__) . '/../'));

include __SITE_APPLICATION_PATH . '/application/' . 'frontController.class.php';

include __SITE_APPLICATION_PATH . '/application/' . 'registry.class.php';

include __SITE_APPLICATION_PATH . '/application/' . 'router.class.php';

include __SITE_APPLICATION_PATH . '/application/' . 'view.class.php';

include __SITE_APPLICATION_PATH . '/application/' . 'request.class.php';

include __SITE_APPLICATION_PATH . '/application/' . 'response.class.php';

include __SITE_APPLICATION_PATH . '/application/' . 'baseModel.class.php';


function __autoload($class_name)
{
	$filename = $class_name . '.php';
	if (__MODULE == 'true') {
		$file = __SITE_PATH . str_replace("Model", "", $class_name) . '/model/' . $filename;
	}else{
		$file = __SITE_APPLICATION_PATH . '/model/' . $filename;
	}
	if (file_exists($file) == false) {
		return false;
	}
	include($file);
}

$registry = new registry;

$registry->router = new router($registry);
if (__MODULE == 'false') {
	$registry->router->setPath(__SITE_PATH . '/controller');
}
$registry->response = new Response($registry);
$registry->response->view = new view($registry);
$registry->request = new Request($registry);
$dbini = new iniFile('dbconfig.ini');
try {
	$settings = $dbini->getRule(array('pdo', 'host', 'user', 'password', 'db'));
} catch(Exception $e) {
	include __SITE_APPLICATION_PATH . '/include/' . 'Exception.php';
	die();
}
$registry->db = dbManipulator::getInstance($settings['pdo'], $settings['host'], $settings['user'], $settings['password'], $settings['db']);

?>